package mp;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ms.education.ucenter.UcenterApplication;

import com.ms.education.ucenter.entity.Teacher;
import com.ms.education.ucenter.entity.User;
import com.ms.education.ucenter.service.TeacherService;
import com.ms.education.ucenter.service.UserService;
import com.ms.education.ucenter.service.feign.AclFeignService;
import com.ms.education.ucenter.service.feign.OssFeignService;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.l;


/**
 * <p>Project:education-test01
 * <p>Powered by ms on 2023-01-12 14:25:39
 *
 * @author ms
 * @version 1.0
 * @since 1.8
 */

@SpringBootTest(classes = UcenterApplication.class)
public class test01 {

    @Autowired
    private UserService userService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private OssFeignService ossFeignService;

    String avatar = "https://msjava.oss-cn-qingdao.aliyuncs.com/2022/01/08/%E9%BB%91%E5%AE%A2.gif";

    @Autowired
    private AclFeignService aclFeignService;

    @Test
    public void insert() {
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.set("avatar", avatar);
        userService.update(wrapper);
    }

    //批量修改用户和讲师头像
    @Test
    public void test1() throws Exception {
        String path = "D:\\Pictures\\毕设图文\\讲师";
        String[] names = new File(path).list();
        ArrayList<String> urls = new ArrayList<>();
        for (String name : names) {
            MultipartFile multipartFile = getMultipartFile(new File(path + "\\" + name));
            String url = (String) ossFeignService.uploadPhoto(multipartFile).getData().get("url");
            urls.add(url);
        }
        List<User> users = userService.list();
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            user.setAvatar(urls.get(i));
        }
        userService.updateBatchById(users);

        List<Teacher> teachers = teacherService.list();
        for (int i = 0; i < teachers.size(); i++) {
            Teacher teacher = teachers.get(i);
            teacher.setAvatar(urls.get(i));
        }
        teacherService.updateBatchById(teachers);


    }

    @org.junit.Test
    public void test2() throws Exception {
        // 路径
        String path = "D:\\Pictures\\毕设图文\\讲师";
        String[] list = new File(path).list();
        System.out.println(Arrays.toString(list));
    }

    // File转MultipartFile
    public static MultipartFile getMultipartFile(File file) {
        DiskFileItem item = new DiskFileItem("file"
                , MediaType.MULTIPART_FORM_DATA_VALUE
                , true
                , file.getName()
                , (int) file.length()
                , file.getParentFile());
        try {
            OutputStream os = item.getOutputStream();
            os.write(FileUtils.readFileToByteArray(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new CommonsMultipartFile(item);
    }
}
